<template>
  <div class="goods-comment-image">
    <!-- 图片列表 -->
    <div class="list">
      <a
        href="javascript:;"
        :class="{active:currImage===url}"
        @click="currImage=url"
        v-for="url in pictures"
        :key="url"
      >
        <img :src="url" alt="">
      </a>
    </div>
    <!-- 大图预览区 刚打开页面 不显示大图-->
    <div class="preview" v-if="currImage">
        <img :src="currImage" />
        <i @click = "currImage = null" class="iconfont icon-close-new"></i>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
export default {
  name: 'GoodsCommentImage',
  props: {
    pictures: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    // 当前显示图片url
    const currImage = ref(null)

    // 实现大图预览
    // 思路:
    // 1. 点击的时候把当前点击的图片的url存下来 然后在大盒子中通过img src渲染 出来
    // 2. 点击叉号 关闭大图 通过v-if绑定响应式数据 控制currImage为null时移除dom
    // 3. 存储点击图片url的响应式数据默认情况下为空，为空 展示图片的大盒子就不显示

    return { currImage }
  }
}
</script>
<style scoped lang="less">
.goods-comment-image {
  .list {
    display: flex;
    flex-wrap: wrap;
    margin-top: 10px;
    a {
      width: 120px;
      height: 120px;
      border:1px solid #e4e4e4;
      margin-right: 20px;
      margin-bottom: 10px;
      img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
      &.active {
        border-color: @xtxColor;
      }
    }
  }
}

.preview {
    position: relative;
    width: 480px;
    height: 480px;
    border: 1px solid #e4e4e4;
    background: #f8f8f8;
    margin-bottom: 20px;
    img {
      width: 100%;
      height: 100%;
      object-fit: contain;
    }
    .icon-close-new {
      position: absolute;
      right: 0;
      top: 0;
      width: 30px;
      height: 30px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      text-align: center;
      line-height: 30px;
    }
}
</style>
